package cn.org.timespace.easyframe.dao.permission.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;

import cn.org.timespace.easyframe.dao.EfBaseDAO;
import cn.org.timespace.easyframe.dao.permission.EfRoleMenuShortcutDAO;
import cn.org.timespace.easyframe.model.permission.EfRoleShortcutmenu;

/**
 * <p>
 * 角色快捷菜单
 * </p>
 * 
 * @version 1.0, 11/02/27
 * @since 1.0
 * @author 
 */

public class EfRoleMenuShortcutDAOImpl extends EfBaseDAO implements EfRoleMenuShortcutDAO{

	public List<EfRoleShortcutmenu> findByCondition(String roleId) {
		 
		StringBuffer queryString = new StringBuffer("from EfRoleShortcutmenu a where 1=1");

		if (!StringUtils.isEmpty(roleId)) {
			queryString.append(" and a.efRole.roleId = '"+roleId+"'");
		}
		return getHibernateTemplate().find(queryString.toString());
	}

	public void save(EfRoleShortcutmenu transientInstance) {
		log.debug("saving EfRoleShortcutmenu instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(EfRoleShortcutmenu persistentInstance) {
		log.debug("deleting EfRoleShortcutmenu instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public void delete(String roleId) {
		log.debug("deleting EfRoleMenu by roleId");
		try {
			StringBuffer deleteSql = new StringBuffer("delete from EfRoleShortcutmenu a where a.efRole.roleId ='"+roleId+"' ");
			Query q = getSession().createQuery(deleteSql.toString());
			int i = q.executeUpdate();
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}
	
	/**
	 * 根据角色ID，找出他所有的角色快捷菜单表
	 * */
	
	public List<EfRoleShortcutmenu> findEfRoleShortcutmenuByRoleId(String roleId) {
		StringBuffer queryString = new StringBuffer("from EfRoleShortcutmenu a where 1=1 ");

		if (!StringUtils.isEmpty(roleId)) {
			queryString.append(" and a.efRole.roleId= '" + roleId + "'");
		}

		return getHibernateTemplate().find(queryString.toString());
	}
	
}